package com.rxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rxt.common.exception.CustomException;
import com.rxt.mapper.PProductSkuMapper;
import com.rxt.model.domain.PProduct;
import com.rxt.mapper.PProductMapper;
import com.rxt.model.domain.PProductSku;
import com.rxt.model.dto.UUserDto;
import com.rxt.model.vo.ProductVo;
import com.rxt.service.IPProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rxt.util.BaseContext;
import com.rxt.util.BigDecimalComparator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 周梓文
 * @since 2023-08-24
 */
@Slf4j
@Service
public class PProductServiceImpl extends ServiceImpl<PProductMapper, PProduct> implements IPProductService {

    @Autowired
    private PProductMapper productMapper;

    @Autowired
    private PProductSkuMapper productSkuMapper;

    @Override
    public List<PProduct> getHotProd() {
        return productMapper.getHot();
    }

    @Override
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            PProduct product = productMapper.selectById(id);
            if (product.getPublishStatus() == 1) {
                throw new CustomException(2001,"上架中的商品不能删除!");
            }
        }
        for (Integer id : ids) {
            productMapper.deleteById(id);
        }

    }

    @Override
    public ProductVo getDetails(Integer id) {
        PProduct product = productMapper.selectById(id);
        ProductVo productVo = new ProductVo();
        BeanUtils.copyProperties(product,productVo);
        log.info("productVo{}",productVo);
        QueryWrapper<PProductSku> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id",id);
        List<PProductSku> productSkus = productSkuMapper.selectList(wrapper);
        log.info("skus:{}",productSkus);
        productVo.setSkus(productSkus);
        return productVo;
    }

    @Override
    public PProductSku getPrice(Integer id,String sku) {
        QueryWrapper<PProductSku> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id",id)
                .eq("sku",sku);
        return productSkuMapper.selectOne(wrapper);
    }

    @Override
    public List<PProduct> getMyProd() {
        UUserDto userDto = BaseContext.get();
        LambdaQueryWrapper<PProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PProduct::getUserId,userDto.getId());
        return productMapper.selectList(wrapper);
    }

    @Override
    public void addProd(ProductVo productVo) {
        PProduct product = new PProduct();
        product.setUserId(BaseContext.get().getId());
        product.setTitle(productVo.getTitle());
        product.setDescription(productVo.getDescription());
        product.setPic(productVo.getPic());
        product.setMinPrice(BigDecimal.valueOf(0));
        product.setMaxPrice(BigDecimal.valueOf(0));
        productMapper.insert(product);
        List<BigDecimal> prices = new ArrayList<>();
        for (PProductSku skus : productVo.getSkus()) {
            PProductSku productSku = new PProductSku();
            productSku.setProductId(product.getId());
            productSku.setSku(skus.getSku());
            productSku.setPrice(skus.getPrice());
            productSkuMapper.insert(productSku);
            prices.add(skus.getPrice());
        }
        Collections.sort(prices);
        product.setMinPrice(prices.get(0));
        product.setMaxPrice(prices.get(prices.size()-1));
        productMapper.updateById(product);
    }
}
